<?php
namespace app\supplier\controller;

use app\erp\model\Branddb;
use app\erp\model\Sitedb;
use app\erp\model\BrandApidb;

class Brand extends Base
{

    protected $db;

    protected $cols = array(
        array(
            'field' => 'brand_id',
            'width' => 40,
            'title' => 'ID',
            'type' => 'checkbox',
            'fixed' => 'left'
        ),
        array(
            'field' => 'brand_name',
            'width' => 160,
            'title' => '品牌名称',
            'sort' => 'true',
            'edit' => 'true'
        ),
        array(
            'field' => 'brand_sign',
            'width' => 160,
            'title' => '品牌标识',
            'sort' => 'true'
        ),
        array(
            'field' => 'api_option',
            'width' => 120,
            'title' => '接口参数',
            'edit' => true
        ),
        array(
            'field' => 'initails',
            'width' => 160,
            'title' => '英文缩写',
            'sort' => 'true',
            'edit' => 'true'
        ),
        array(
            'field' => 'brand_picture',
            'width' => 160,
            'title' => '品牌LOGO',
            'templet' => '#brand_pictureTpl'
        ),
        array(
            'field' => 'audit_state',
            'width' => 80,
            'title' => '审核',
            'templet' => '#audit_stateTpl'
        ),
        array(
            'fixed' => 'right',
            'width' => 110,
            'toolbar' => '#brand_tool'
        )
    );

    public $post = array();

    public function _initialize()
    {
        parent::_initialize();
        $request = request();
        $param = $request->param();
        $this->post = $param;
        $this->db = new Branddb();
    }

    public function index()
    {
        // 获取分站列表
        $sitedb = new Sitedb();
        $get_site = $sitedb->ajaxFind(SITE_ID);
        $this->assign('get_site', $get_site);
        $sitedb->param['_o'] = 'site_id';
        $sitedb->param['_b'] = 'asc';
        $sitedb->param['page'] = 1;
        $sitedb->param['limit'] = 1000;
        $get_site_list = $sitedb->ajaxSelect();
        $this->assign('site_list', $get_site_list['data']);
        $this->assign('cols', json_encode($this->cols));
        return $this->fetch();
    }

    public function tplForm()
    {
        $brand_sign = 0;
        $data = array();
        if (isset($this->post['brand_sign'])) {
            $data = $this->db->ajaxFind($this->post['brand_sign']);
            $brand_sign = $this->post['brand_sign'];
        }
        $this->assign('data', json_encode($data));
        $this->assign('brand_sign', $brand_sign);
        return $this->fetch();
    }

    /**
     *
     * @todo 保存数据
     */
    public function saveData()
    {
        $this->db->param = $this->post;
        $return = $this->db->ajaxSave();
        if ($return != 'success') {
            $this->layuiReturn($return, 0, $return, - 1);
        } else {
            $this->layuiReturn('保存成功');
        }
    }

    /**
     *
     * @todo 列表
     */
    public function ajaxList()
    {
        $this->db->param = $this->post;
        $return = $this->db->ajaxSelect();
        $this->layuiReturn($return['data'], $return['count']);
    }

    /**
     *
     * @todo 获取单条数据
     */
    public function ajaxOneData()
    {
        $data = $this->db->ajaxFind($this->post['brand_sign']);
        $this->layuiReturn($data, 1);
    }

    /**
     *
     * @todo 软删除
     */
    public function ajaxDelete()
    {
        $this->db->param = $this->post;
        $this->db->ajaxDelete();
        $this->layuiReturn('删除成功', 0);
    }

    public function upload()
    {
        set_time_limit(3600);
        ini_set('memory_limit', '256M');
        $files = request()->file();
        vendor("PHPExcel.PHPExcel");
        $phpExcel = new \PHPExcel();
        // 上传文件
        foreach ($files as $file) {
            $info = $file->validate([
                'site' => 1024 * 10 * 1024 * 1024,
                'ext' => 'xls,xlsx'
            ])->move('./uploads/');
            if ($info) {
                $fileName = './uploads/' . $info->getSaveName();
                $objReader = \PHPExcel_IOFactory::createReader('Excel5');
                $objReader->setReadDataOnly(true);
                $objPHPExcel = $objReader->load($fileName, $encode = 'utf-8');
                $sheet = $objPHPExcel->getSheet();
                $get_row_num = $sheet->getHighestRow(); // 总行数
                $get_col_num = $sheet->getHighestColumn(); // 总列
                $phpexcel_shared_date = new \PHPExcel_Shared_Date();
                $data = array();
                $text = '';
                for ($i = 2; $i <= $get_row_num; $i ++) {
                    unset($temp);
                    $the_sheet = $objPHPExcel->getActiveSheet();
                    $temp['brand_name'] = $this->getSheetValue($the_sheet, "A" . $i);
                    if (empty($temp['brand_name'])) {
                        $this->layuiReturn('导入失败', 0, '品牌名称（第'.$i.'行）不能为空，请检查excel文档', 0);
                    }
                    $temp['initails'] = $this->getSheetValue($the_sheet, "B" . $i);
                    $temp['partner_id'] = PARTNER_ID;
                    foreach ($data as $k => $v) {
                        if ($v['brand_name'] == $temp['brand_name']) {
                            $this->layuiReturn('导入失败', 0, $v['brand_name'] . '（第'.$i.'行）重复，请检查excel文档', 0);
                        }
                    }
                    array_push($data, $temp);
                }
                $this->db->param = $data;
                $return = $this->db->ajaxSaveAll();
                if ($return == 'success') {
                    $this->layuiReturn('导入成功', 0, '导入成功', 1);
                } else {
                    $this->layuiReturn('导入失败', 0, $return, 0);
                }
            } else {
                $this->layuiReturn($file->getError(), 0, $file->getError(), 0);
            }
        }
    }
    public function siteData()
    {
        if (! isset($this->post['brand_sign'])) {
            exit('参数错误');
        }
        $sitedb = new Sitedb();
        $sitedb->param['_o'] = 'site_id';
        $sitedb->param['_b'] = 'asc';
        $sitedb->param['page'] = 1;
        $sitedb->param['limit'] = 50;
        $get_site_list = $sitedb->ajaxSelect();
        $temp['site_id'] = '';
        $temp['site_name'] = '主站';
        $data = $get_site_list['data'];
        array_push($data, $temp);
        $brandApidb = new BrandApidb();
        foreach ($data as $key=>$val){
            $brandApidb->param = array();
            $brandApidb->param['site_id'] = $val['site_id'];
            $brandApidb->param['brand_sign'] = $this->post['brand_sign'];
            $chk = $brandApidb->ajaxFind();
            $data[$key]['api_option'] = $chk['option'];
            $data[$key]['brand_sign'] = $this->post['brand_sign'];
        }
        $this->assign('site_list', $data);
        return $this->fetch();
    }
    public function saveOption(){
        if (! isset($this->post['brand_sign']) || !isset($this->post['field']) || !isset($this->post['value'])) {
            $this->layuiReturn('参数错误', 0, '参数错误', 0);
        }
        $brandApidb = new BrandApidb();
        $brandApidb->param['site_id'] = $this->post['site_id'];
        $brandApidb->param['brand_sign'] = $this->post['brand_sign'];
        $brandApidb->param['option'] = $this->post['value'];
        $brandApidb->ajaxSave();
        $this->layuiReturn('保存成功',1,'保存成功',1);
    }
}